import os
from dotenv import load_dotenv
import asyncio
from camel.toolkits.mcp_toolkit import MCPToolkit
from camel.agents import ChatAgent
from camel.models import ModelFactory
from camel.types import ModelPlatformType
load_dotenv()
Model_Type=os.getenv("MODEL_TYPE")
Model_Api=os.getenv("ZHIPU_API_KEY")
Model_Url=os.getenv("MODEL_URL")
model = ModelFactory.create(
    model_platform=ModelPlatformType.OPENAI,
    model_type=Model_Type,
    api_key=Model_Api,
    url=Model_Url
)

async def run_mcp_client_example():
    # 1. 初始化 MCPToolkit，指定配置文件路径
    mcp_toolkit = MCPToolkit(config_path="config/mcp_tools.json", timeout=1200)

    # 2. 异步连接到配置文件中定义的所有 MCP 服务器
    await mcp_toolkit.connect()

    # 3. 创建 CAMEL Agent 实例，并将从 MCPToolkit 获取的工具列表传递给 Agent
    camel_agent = ChatAgent(
        model=model,
        tools=[*mcp_toolkit.get_tools()], # 动态获取并解包 MCP 工具列表
    )

    # 4. Agent 执行任务，可能调用 MCP 工具
    response = await camel_agent.astep("帮我搜集一些近日网络上的热点事件")
    print(f"Agent response: {response.msgs[0].content}")
    if response.info and 'tool_calls' in response.info:
        print(f"Tool calls: {response.info['tool_calls']}")

    await mcp_toolkit.disconnect()

asyncio.run(run_mcp_client_example()) 
